package com.ruoyi.service.impl;

import com.ruoyi.domain.Base64;
import com.ruoyi.mapper.Base64Mapper;
import com.ruoyi.service.Base64Service;
import com.ruoyi.utils.SimpleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;

@Service
public class Base64Impl implements Base64Service {
    private final String FILE_PATH = "D:/ruoyi";
    private final String BERFOR_BASE64 = "data:image/png;base64,";
    @Autowired
    private Base64Mapper base64Mapper;

    @Override
    public SimpleResult fileToBase64(MultipartFile file) {
        if(Objects.isNull(file)) {
            return SimpleResult.error("文件为空");
        }
        try {
            /**
             * 把数据全部读取
             */
            InputStream inputStream = file.getInputStream();
            byte[] bytes = readAllBytes(inputStream);
//            byte[] bytes = inputStream.readAllBytes();
//            String base64Data = Base64.getEncoder().encodeToString(bytes);
            inputStream.close();
            /**
             * 把数据保存到目标目录
             */
            File distory = new File(FILE_PATH);
            if(!distory.exists()) {
                distory.mkdirs();
            }
            File saveFile = new File(FILE_PATH + "/" + file.getOriginalFilename());
            FileOutputStream outputStream = new FileOutputStream(saveFile);
            outputStream.write(bytes);
            outputStream.close();
            com.ruoyi.domain.Base64 base64 = new com.ruoyi.domain.Base64(
                    System.currentTimeMillis() + "",
                    null,
                    new Random().nextInt(400) + 100 + "",
                    new Random().nextInt(400) + 100 + "",
                    file.getOriginalFilename()
            );
            int insert = base64Mapper.insert(base64);
            return SimpleResult.success(insert>0?"上传成功":"上传失败");
        } catch (Exception e) {
            e.printStackTrace();
            return SimpleResult.error("上传失败, 文件过大");
        }
    }

    @Override
    public SimpleResult getBase64List() {
        List<com.ruoyi.domain.Base64> base64 = base64Mapper.getBase64List();
        if (base64.size() == 0) {
            return SimpleResult.error("暂无数据");
        }
        for (com.ruoyi.domain.Base64 base : base64) {
            String filePath = base.getFilePath();
            File file = new File(FILE_PATH + "/" + filePath);
            if(!file.exists()) {
                base.setBase64Url("文件丢失");
                continue;
            }
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] bytes = readAllBytes(fileInputStream);
                String base64Data = java.util.Base64.getEncoder().encodeToString(bytes);
                base.setBase64Url(BERFOR_BASE64 + base64Data);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return SimpleResult.success(base64, "获取成功");
    }

    public byte[] readAllBytes(InputStream inputStream) {
        int length = 0;
        ArrayList<byte[]> byteArray = new ArrayList<>();
        byte[] bytes = new byte[1024];
        try {
            int read = inputStream.read(bytes);
            byteArray.add(bytes);
            length += read;
            while (read != -1) {
                bytes = new byte[1024];
                read = inputStream.read(bytes);
                byteArray.add(bytes);
                length += bytes.length;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] res = new byte[length];
        int currentLength = 0;
        for (byte[] b : byteArray) {
            System.arraycopy(b, 0, res, currentLength, b.length);
            currentLength += b.length;
        }
        return res;
    }
}
